package com.ls.lserp.modules.material.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ls.lserp.modules.sys.utils.SystemMessageUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.material.entity.SlopworkOutbound;
import com.ls.lserp.modules.material.service.SlopworkOutboundService;
import freemarker.template.Configuration;
import groovy.util.ResourceException;

@Controller
@RequestMapping(value = "${adminPath}/slopwork/outbound/")
public class SlopworkOutboundController extends BaseController {

    // 保存 编辑 删除 方法
    @Autowired
    private SlopworkOutboundService slopworkOutboundService;
    @Autowired
    Configuration freemarkerConfiguration;

    @ModelAttribute
    public SlopworkOutbound get(@RequestParam(required = false) String outRequestDetailId) {
        // 判断id是否为空
        if (StringUtils.isNotBlank (outRequestDetailId)) {
            // 执行查询语句 查询基本信息
            return slopworkOutboundService.get (Long.parseLong (outRequestDetailId));
        } else {
            // 若为空，实例化
            return new SlopworkOutbound ();
        }
    }

    /**
     * @return url
     * @Title: 画面跳转用
     */
    @RequiresPermissions("slopwork:outbound:view")
    @RequestMapping(value = {""})
    public String list() {
        // 画面跳转
        return "modules/material/slopworkOutbound";
    }

    /**
     * @param slopworkOutbound
     * @param request
     * @param response
     * @param model
     * @return searchList
     * @throws Exception
     * @Title: 画面检索
     * @Description: 查询页面
     */
    @RequiresPermissions("slopwork:outbound:view")
    @RequestMapping(value = {"searchList"})
    @ResponseBody
    public String searchList(SlopworkOutbound slopworkOutbound, HttpServletRequest request, HttpServletResponse response,
                             Model model) throws Exception {
        Map <Object, Object> listMap = new HashMap <Object, Object> ();
        // ajax回调时使用的一览list名
        request.setAttribute (Global.LIST_NAME, "slopworkOutbound");
        // 拼接字符串供分页使用
        StringBuilder searchParam = new StringBuilder ();
        // 入库单号
        searchParam.append ("&outboundRequestNo=");
        searchParam.append (slopworkOutbound.getOutboundRequestNo ());
        // 客户款号
        searchParam.append ("&ocustomerStyleNo=");
        searchParam.append (slopworkOutbound.getOcustomerStyleNo ());
        // 日期
        searchParam.append ("&dateFrom=");
        searchParam.append (slopworkOutbound.getDateFrom ());
        searchParam.append ("&dateTo=");
        searchParam.append (slopworkOutbound.getDateTo ());
        // 状态
        searchParam.append ("&tabPageStatus=");
        searchParam.append (slopworkOutbound.getTabPageStatus ());
        // 放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
        request.setAttribute (Global.SEARCH_PAEAM, searchParam.toString ());
        // 查询一览页面数据
        Page <SlopworkOutbound> page = slopworkOutboundService.findList (new Page <SlopworkOutbound> (request, response), slopworkOutbound);
        // 读取系统路径
        String strUri = request.getContextPath () + Global.getAdminPath ();
        // 读取文件生成字符串
        String strHtml = "";
        Map <String, Object> map = new HashedMap ();
        map.put ("adminUri", strUri);
        map.put ("slopworkOutbounds", page.getList ());
        map.put ("pageNo", page.getPageNo ());
        map.put ("pageSize", page.getPageSize ());
        strHtml = FreeMarkerTemplateUtils
                .processTemplateIntoString (freemarkerConfiguration.getTemplate ("slopworkOutbound.ftl"), map);
        listMap.put ("slopworkOutbound", strHtml);
        listMap.put ("page", page.getHtml ());
        // ajax返回结果
        return renderString (response, listMap);
    }

    /**
     * 进入查看页面
     *
     * @param slopworkOutbound
     * @param model
     * @return
     */
    @RequiresPermissions("slopwork:outbound:view")
    @RequestMapping(value = "show")
    public String show(SlopworkOutbound slopworkOutbound, Model model) {
        // 出库申请
        SlopworkOutbound resultRequest = slopworkOutboundService.getClothesRequest (slopworkOutbound);
        slopworkOutbound.setOutboundCategory(resultRequest.getOutboundCategory());
        // 查找出库申请明细
        List <SlopworkOutbound> resultRequestList = slopworkOutboundService.findShowRequestDetailList (slopworkOutbound);
        resultRequest.setClothsOutDetailsList (resultRequestList);
        // 将查询结果赋值mvc model层返回jsp页面
        model.addAttribute ("slopworkOutbound", resultRequest);
        // 返回页面地址
        return "modules/material/slopworkOutboundShow";
    }

    /**
     * 进入查看页面
     *
     * @param slopworkOutbound
     * @param model
     * @return
     */
    @RequiresPermissions("slopwork:outbound:view")
    @RequestMapping(value = "inventoryClothesDetail")
    public String inventoryClothesDetail(SlopworkOutbound slopworkOutbound, Model model) {
        // 查找出库申请明细
        List <SlopworkOutbound> resultRequestList = slopworkOutboundService.findClothesInventoryDetails (slopworkOutbound);
        slopworkOutbound.setClothsOutDetailsList (resultRequestList);
        // 将查询结果赋值mvc model层返回jsp页面
        model.addAttribute ("slopworkOutbound", slopworkOutbound);
        // 返回页面地址
        return "modules/material/slopworkOutboundDetails";
    }

    /**
     * 保存信息
     *
     * @param slopworkOutbound
     * @param response
     * @return
     * @throws ResourceException
     */
    @RequiresPermissions("slopwork:outbound:edit")
    @RequestMapping(value = "save")
    @ResponseBody
    public String save(SlopworkOutbound slopworkOutbound, HttpServletResponse response) throws ResourceException {
        // 保存实际出库数量
        slopworkOutboundService.saveSlopworkDetail (slopworkOutbound);
        // 保存成功返回提示信息
        return renderString (response, AjaxDone.ok (SystemMessageUtils.getMessageName ("warehouse_edit_save_msg_success")));
    }

    /**
     * 保存数量
     *
     * @param slopworkOutbound
     * @param response
     * @return
     * @throws ResourceException
     */
    @RequiresPermissions("slopwork:outbound:edit")
    @RequestMapping(value = "saveQuantity")
    @ResponseBody
    public String saveQuantity(SlopworkOutbound slopworkOutbound, HttpServletResponse response) throws ResourceException {
        // 保存入库申请
        slopworkOutboundService.saveQuantity (slopworkOutbound);
        // 保存成功返回提示信息
        return renderString (response, AjaxDone.ok (SystemMessageUtils.getMessageName ("warehouse_edit_save_msg_success")));
    }

    /**
     * 创建物料入库信息窗口
     *
     * @return
     */
    @RequiresPermissions("slopwork:outbound:edit")
    @RequestMapping(value = {"changeStatus"})
    public String changeStatus(SlopworkOutbound slopworkOutbound, Model model) {
        slopworkOutboundService.changeStatus (slopworkOutbound);
        return "modules/material/slopworkOutboundShow";
    }
}
